<?php
/**
 * Created by PhpStorm.
 * User: junyv
 * Date: 2017/3/30
 * Time: 9:07
 */

namespace app\home\controller;


class Xiaoqu extends Base
{
    public function _initialize(){
        parent::_initialize();
        $id = input('param.id/d',0);
        $this->assign('action',strtolower(request()->action()));
        $this->assign('id',$id);
    }
    //列表页
    public function index(){
        $this->search();
        $this->getAttr();
        $this->getLists();
        $this->assign('niandai',$this->getNiandai());
        return $this->fetch();
    }
    //楼盘详细页
    public function detail(){
        $id   = input('param.id/d',0);
        $info = model('estate')->where(['status'=>1,'id'=>$id])->find();
        if(!$id || !$info){
            return $this->fetch('public/404');
        }
        $info = $info->toArray();
        $estate  = model('estate_data')->where(['id'=>$info['id']])->find()->toArray();
        model('estate')->where(['id'=>$id])->setInc('hits');
        $this->setSeo($info);
        $this->getAreaEstate($info['city'],$info['area']);//同区域小 区
        $this->assign('info',array_merge($info,$estate));
        return $this->fetch();
    }
    //小区二手房
    public function used(){
        $id   = input('param.id/d',0);
        $info = model('estate')->where(['status'=>1,'id'=>$id])->find();
        if(!$id || !$info){
            return $this->fetch('public/404');
        }
        $this->setSeo(['title'=>$info['title'].'二手房']);
        $this->assign('info',$info);
        return $this->fetch();
    }
    //小区出租房
    public function rental(){
        $id   = input('param.id/d',0);
        $info = model('estate')->where(['status'=>1,'id'=>$id])->find();
        if(!$id || !$info){
            return $this->fetch('public/404');
        }
        $this->setSeo(['title'=>$info['title'].'出租房']);
        $this->assign('info',$info);
        return $this->fetch();
    }
    //小区详细
    public function info(){
        $id   = input('param.id/d',0);
        $info = model('estate')->where(['status'=>1,'id'=>$id])->find()->toArray();
        if(!$id || !$info){
            return $this->fetch('public/404');
        }
        $estate  = model('estate_data')->where(['id'=>$info['id']])->find()->toArray();
        $this->getAreaEstate($info['city'],$info['area']);
        $this->assign('info',array_merge($info,$estate));
        $this->setSeo(['title'=>$info['title'].'详细信息']);
        return $this->fetch();
    }
    public function pic(){
        $id   = input('param.id/d',0);
        $info = model('estate')->where(['status'=>1,'id'=>$id])->find();
        if(!$id || !$info){
            return $this->fetch('public/404');
        }
        $this->setSeo(['title'=>$info['title'].'相册']);
        $this->assign('info',$info);
        return $this->fetch();
    }
    private function getAreaEstate($city,$area){
        $city &&  $where['city'] = $city;
        $area && $where['area']  = $area;
        $where['status'] = 1;
        $lists = model('estate')->where($where)->field('id,title')->limit(10)->select();
        $this->assign('sameArea',$lists);
    }
    private function getLists(){
        $where = $this->search();
        $sort  = $this->order();
        $field = 'id,title,img,address,niandai,estate_type,estate_type_name,file,lat,lng,second_num,chuzu_num';
        $lists = model('estate')->where($where)->field($field)->order($sort)->paginate(10);
        $this->assign('lists',$lists);
        $this->assign('pages',$lists->render());
    }
    private function search(){
        $param['area'] = input('param.area',0);
        $param['year']      = input('param.year',0);//建筑年代
        $param['type']       = input('param.type',0);//楼盘类型
        $param['sort']       = input('param.sort/d',0);//排序
        $data['status']      = 1;
        $data['city']        = $this->cityInfo['id'];
        $keyword = input('get.keyword');
        if($keyword){
            $param['keyword'] = $keyword;
            $data['title'] = ['like','%'.$keyword.'%'];
        }

        if(!empty($param['area'])){
            $data['area'] = $param['area'];
        }
        if(!empty($param['year'])){
            $data['niandai'] = $this->getNiandai($param['year']);
        }
        if(!empty($param['type'])){
            $data[] = ['exp',"find_in_set({$param['type']},estate_type)"];
        }
        $this->assign('param',$param);
        $this->assign('search',$param);
        return $data;
    }
   private function getAttr($return=''){
        $house_type     = cache('linkmenu_7');//楼盘类型
        if($return){
          return $house_type['cate'];
        }else{
            $this->assign('house_type',$house_type['cate']);
        }
    }
    private function getNiandai($key=''){
        $y    = date('Y');
        $data = [
            1 => ['name'=>'2年以下','value'=>[$y-2,$y]],
            2 => ['name'=>'2-5年','value'=>[$y-5,$y-2]],
            3 => ['name'=>'5-10年','value'=>[$y-10,$y-5]],
            4 => ['name'=>'10年以上','value'=>$y-10],

        ];
        if($key){
            if($key == 4){
                $str = ['elt',$data[$key]['value']];
            }else{
                $str = ['between',$data[$key]['value']];
            }
        }else{
            $str = $data;
        }
        return $str;
    }
    //排序
    private function order(){
        $sort = input('param.sort/d',0);
        switch($sort){
            case 1:
                $order = 'niandai asc';
                break;
            case 2:
                $order = 'niandai desc';
                break;
            default:
                $order = 'ordid asc,id desc';
                break;
        }
        $this->assign('sort',$sort);
        return $order;
    }

}